/*
 * Copyright 2015 Rudson Lima
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package br.liveo.adapter;

import java.util.List;

import br.liveo.navigationliveo.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.BlendMode;
import ohos.agp.utils.Color;

import ohos.app.Context;

/**
 * NavigationLiveoAdapter
 */
public class NavigationLiveoAdapter extends BaseItemProvider {

    private int mColorName = 0;
    private int mColorCounter = 0;
    private int mColorSeparator = 0;
    private int mColorSubHeader = 0;

    private int mColorDefault = 0;
    private final Context mContext;
    private boolean mRemoveAlpha = false;
    private final List<NavigationLiveoItemAdapter> mList;

    /**
     * NavigationLiveoAdapter
     *
     * @param context context
     * @param list list
     */
    public NavigationLiveoAdapter(Context context, List<NavigationLiveoItemAdapter> list) {
        this.mList = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public NavigationLiveoItemAdapter getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemComponentType(int position) {
        return mList.get(position).isVisible ? 0 : 1;
    }

    /**
     * 设置是否可以点击
     *
     * @param pos 位置
     * @param checked 是否能点击
     */
    public void setChecked(int pos, boolean checked) {
        mList.get(pos).checked = checked;
        notifyDataChanged();
    }

    /**
     * 重写设置点击按钮
     */
    public void resetarCheck() {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).checked = false;
        }
        this.notifyDataChanged();
    }

    /**
     * 设置新的名字
     *
     * @param position 位置
     * @param name 名字
     */
    public void setNewName(int position, String name) {
        mList.get(position).title = name;
        notifyDataChanged();
    }

    /**
     * 设置新的图标
     *
     * @param position 位置
     * @param icon 图标
     */
    public void setNewIcon(int position, int icon) {
        mList.get(position).icon = icon;
        notifyDataChanged();
    }

    /**
     * 设置新的数量
     *
     * @param position 位置
     * @param value 数量
     */
    public void setNewCounterValue(int position, int value) {
        mList.get(position).counter = value;
        notifyDataChanged();
    }

    /**
     * 设置一个新的item
     *
     * @param position 位置
     * @param name 姓名
     * @param icon 图标
     * @param counter 数量
     */
    public void setNewInformationItem(int position, String name, int icon, int counter) {
        mList.get(position).title = name;
        mList.get(position).icon = icon;
        mList.get(position).counter = counter;
        notifyDataChanged();
    }

    /**
     * 设置减去数量
     *
     * @param position 位置
     * @param value 数量
     */
    public void setIncreasingCounterValue(int position, int value) {
        mList.get(position).counter = (mList.get(position).counter + value);
        notifyDataChanged();
    }

    /**
     * setDecreaseCountervalue
     *
     * @param position position
     * @param value value
     */
    public void setDecreaseCountervalue(int position, int value) {
        mList.get(position).counter = (mList.get(position).counter - value);
        notifyDataChanged();
    }

    /**
     * setVisibleItemNavigation
     *
     * @param position position
     * @param visible visible
     */
    public void setVisibleItemNavigation(int position, boolean visible) {
        mList.get(position).isVisible = visible;
        notifyDataChanged();
    }

    static class ViewHolder {
        public Text title;
        public Image icon;
        public Text counter;

        public Component separator;

        public DependentLayout layoutDados;
        public DirectionalLayout layoutSeparator;

        /**
         * 构造器
         */
        public ViewHolder() {
        }
    }

    public Element getImageDrawable(int res) {
        PixelMapElement drawable = null;
        try {
            drawable = new PixelMapElement(
                    mContext.getResourceManager().getResource(res));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawable;
    }

    /**
     * 更改Element颜色
     *
     * @param element  Element
     * @param intColor 颜色
     * @return Element
     */
    public static Element getTintElement(Element element, int intColor) {
        int[] colors = new int[]{intColor, intColor};
        int[][] states = new int[2][];
        states[0] = new int[]{1};
        states[1] = new int[]{-1};
        element.setStateColorList(states, colors);
        element.setStateColorMode(BlendMode.SRC_ATOP);
        return element;
    }

    private void setAlpha(Component v, float alpha) {
        v.setAlpha(!mRemoveAlpha ? alpha : 1f);
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
        ViewHolder holder;
        holder = new ViewHolder();
        convertView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_navigation_list_item, null, false);
        holder.title = (Text) convertView.findComponentById(ResourceTable.Id_title);
        holder.counter = (Text) convertView.findComponentById(ResourceTable.Id_counter);
        holder.icon = (Image) convertView.findComponentById(ResourceTable.Id_icon);
        holder.separator = convertView.findComponentById(ResourceTable.Id_separator);
        holder.layoutDados = (DependentLayout) convertView.findComponentById(ResourceTable.Id_layoutDados);
        holder.layoutSeparator = (DirectionalLayout) convertView.findComponentById(ResourceTable.Id_layoutSeparator);
        convertView.setTag(holder);
        NavigationLiveoItemAdapter item = mList.get(position);
        if (item.checked) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(224, 224, 224));
            convertView.setBackground(shapeElement);
        }
        if (holder.title != null) {
            holder.title.setText(item.title);
            if (!item.isHeader) {
                holder.layoutSeparator.setVisibility(Component.HIDE);
                int type = getItemComponentType(position);
                holder.layoutDados.setVisibility(type == 0 ? Component.VISIBLE : Component.HIDE);
                setAlpha(holder.title, (item.checked ? 1f : 0.87f));
            } else {
                holder.layoutSeparator.setVisibility(Component.VISIBLE);
                if (mColorSeparator > 0) {
                    ShapeElement shapeElement = new ShapeElement();

                    shapeElement.setRgbColor(new RgbColor(mColorSeparator));
                    holder.separator.setBackground(shapeElement);
                }
                if (item.title.equals("")) {
                    holder.title.setVisibility(Component.HIDE);
                    holder.layoutDados.setVisibility(Component.HIDE);
                } else {
                    holder.layoutDados.setVisibility(Component.VISIBLE);
                }
            }
            int intColor = (!item.isHeader && item.checked && item.colorSelected < 0 ?
                    item.colorSelected :
                    mColorDefault != 0 ? mColorDefault :
                            mColorName > 0 ? mColorName :
                                    (item.isHeader && mColorSubHeader > 0) ? mColorSubHeader :
                                            ResourceTable.Color_nliveo_black);
            Color color = new Color(intColor);
            holder.title.setTextColor(color);
        }
        if (holder.counter != null) {
            if (item.counter >= 1) {
                setAlpha(holder.counter, (item.checked ? 1f : 0.87f));
                holder.counter.setVisibility(Component.VISIBLE);
                holder.counter.setText((item.counter > 99) ? "99+" : item.counter + "");
                int i1 = !item.isHeader && item.checked && item.colorSelected < 0 ?
                        item.colorSelected :
                        mColorDefault != 0 ? mColorDefault :
                                mColorCounter > 0 ?
                                        mColorCounter :
                                        ResourceTable.Color_nliveo_black;
                Color color = new Color(i1);
                holder.counter.setTextColor(color);
            } else {
                holder.counter.setVisibility(Component.HIDE);
                if (item.counter < 0) {
                    setNewCounterValue(position, 0);
                }
            }
        }
        if (holder.icon != null) {
            if (item.icon != 0) {
                holder.icon.setVisibility(Component.VISIBLE);
                setAlpha(holder.icon, (!item.isHeader && item.checked ? 1f : 0.54f));
                holder.icon.setPixelMap(item.icon);
                if (!item.isHeader && item.checked) {
                    Element imageDrawable = getImageDrawable(item.icon);
                    holder.icon.setImageElement(getTintElement(imageDrawable, item.colorSelected));
                }
            } else {
                holder.icon.setVisibility(Component.HIDE);
            }
        }
        return convertView;
    }
}